package com.easysoft.framework.user.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.user.mapper.SysUserMapper;
import com.easysoft.framework.user.mapper.SysUserMapperExtd;
import com.easysoft.framework.user.model.SysUser;
import com.easysoft.framework.user.model.SysUserQuery;
import com.easysoft.framework.user.model.SysUserView;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户模块
 * <p>
 * Created by EasySoft on 2017/6/14.
 */
@Service("service.UserService")
public class UserService {

    @Autowired
    @Qualifier("mapper.SysUserMapperExtd")
    private SysUserMapperExtd sysUserMapperExtd;

    @Autowired
    @Qualifier("mapper.SysUserMapper")
    private SysUserMapper sysUserMapper;

    /**
     * 根据名字查询用户
     *
     * @param username
     * @return
     * @throws Exception
     */
    @DataSource(DataSource.SLAVE)
    public SysUserView selectUserByUserName(String username) {

        return sysUserMapperExtd.selectUserByUserName(username);
    }

    /**
     * 根据用户名和手机号查询用户
     *
     * @param username
     * @param mobile
     * @return list
     */
    @DataSource(DataSource.SLAVE)
    public List<SysUserView> selectUserByUserNameOrMobile(@Param("username") String username, @Param("mobile") String mobile) {
        return sysUserMapperExtd.selectUserByUserNameOrMobile(username, mobile);
    }

    /**
     * 新增用户
     *
     * @param sysUser
     * @return
     */
    @DataSource(DataSource.MASTER)
    public int addSysUser(SysUser sysUser) {
        sysUser.setId(UUIDUtils.getUUID());
        sysUser.setCreateTime(new Date());
        return sysUserMapper.insertSelective(sysUser);
    }

    /**
     * 根据userId查询用户信息
     *
     * @param userId 用户id
     *
     * @return 用户信息
     */
     @DataSource(DataSource.SLAVE)
     public  SysUser selectSysUserByUserId(String userId){

        return  sysUserMapper.selectByPrimaryKey(userId);
     }

    @DataSource(DataSource.SLAVE)
     public SysUserView selectSysUserViewById(String id){
         return sysUserMapperExtd.selectSysUserViewById(id);
     }


    /**
     * 获取用户列表
     *
     * @param query 查询条件参数
     * @param page  分页参数
     * @return 用户的list集合
     */
    @DataSource(DataSource.SLAVE)
    public List<SysUserView> selectSysUserListByPage(SysUserQuery query, Page page) {
        int maxRow = sysUserMapperExtd.selectCountSysUser(query);
        if (maxRow == 0) {
            return new ArrayList<SysUserView>();
        }
        page.setMaxRow(maxRow);
        return sysUserMapperExtd.selectSysUserListByPage(page, query);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysUser(String id){
        return sysUserMapperExtd.deleteByPrimaryKey(id);
    }

    /**
     * 修改用户
     * @param sysUser 用户参数
     *
     * @return 返回结果集
     */
    @DataSource(DataSource.MASTER)
    public int updateSysUserById(SysUser sysUser){
        return  sysUserMapper.insertSelective(sysUser);
    }

    @DataSource(DataSource.SLAVE)
    public List<SysUserView> selectAllSysUser() {
        return sysUserMapperExtd.selectAllSysUser();
    }

    @DataSource(DataSource.SLAVE)
    public int countUserByUserNameOrMobile(String username, String mobile) {
        return sysUserMapperExtd.countUserByUserNameOrMobile(username, mobile);
    }
}
